//
//  HNApplyRefundController.m
//  baidaifu
//
//  Created by 朱传正 on 2017/2/6.
//  Copyright © 2017年 haoniu. All rights reserved.
//

#import "HNApplyRefundController.h"
#import "HNApplyRefundSuccessController.h"

@interface HNApplyRefundController ()<UITextViewDelegate>

@end

@implementation HNApplyRefundController


- (IBAction)submitApplyAction:(UIButton *)sender {
    
    if ([AppConfig isNULL:_applyReason.text]) {
        [self showHint:@"请输入退款理由"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_orderSn.text forKey:@"orderSn"];
    [param setObject:_applyReason.text forKey:@"refundReason"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_order_applyRefund parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"申请退款失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            
            HNApplyRefundSuccessController *applyRefundSuccessVC = [[HNApplyRefundSuccessController alloc]init];
            [self.navigationController pushViewController:applyRefundSuccessVC animated:YES];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"申请退款失败"];
    }];

    

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请退款";
    
    _applyReason.delegate = self;
    
    _submitApplyBtn.backgroundColor = RGB(85, 206, 84);
    _submitApplyBtn.layer.masksToBounds = YES;
    _submitApplyBtn.layer.cornerRadius = 4;
    
    
    _applyReasonBorder.layer.masksToBounds = YES;
    _applyReasonBorder.layer.borderWidth = 0.5;
    _applyReasonBorder.layer.cornerRadius = 5;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){189.0/255.0,189.0/255.0,189.0/255.0,1});
    
    [_applyReasonBorder.layer setBorderColor:color];

}

-(void)drawView{
    _orderSn.text = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"orderSn"]];
    _orderState.text = @"服务中";
    _orderType.text = @"图文咨询";
    NSString *sellerNickname = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"sellerNickname"]];
     NSString *sellerUsername = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"sellerUsername"]];
    if ([AppConfig isNULL:sellerNickname]) {
        _doctorName.text = sellerUsername;
    }else{
        _doctorName.text = sellerNickname;
    }
   
    _payTime.text = [NSString stringWithFormat:@"¥%@",[_orderDic objectForKey:@"paidTime"]];
}

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    if (_applyReason.text.length > 0) {
        [self.promptText setHidden:YES];
    }else{
        [self.promptText setHidden:NO];
    }
}


@end
